layui.use(["layer","form","upload","jquery","element"],function () {
    var
        layer = layui.layer,
        form = layui.form,
        element = layui.element,
        $ = layui.jquery,
        upload = layui.upload;
    $("#newAccountUrl").attr("src","");
    var user = localStorage.getItem("user");
    user = JSON.parse(user);
    //头像
    $("img[name=accountUrl]").attr("src",user.data.acatarUrl);
    $("input[name=account]").val(user.data.account);
    $("input[name=email]").val(user.data.email);
    upload.render({
        elem:"#accountUrl",
        url:"/Upload/upload",
        done:function (res) {
            $("img[name=accountUrl]").attr("src",res.data);
            $("img[name=newAccountUrl]").attr("src",res.data);
            var param = {
                "acatarUrl":res.data,
                "userId":user.data.userId
            };
            $.ajax({
                url:"/HomePage/UpdateUser",
                data:param,
                type:'post',
                dataType:"json",
                success:function (data) {
                    if (data.data == true){
                        layer.msg("修改成功");
                    }
                }
            });


        }
    })
    $("#newPassword").blur(function () {
        var jiaoyan = new RegExp(/^[a-zA-Z0-9]{6}$/);
        var newPassword = $("input[name=newPassword]").val();
        console.log(newPassword);
        if(newPassword == "" || newPassword == null){
            $(".NewPasswordjiaoyanText").text("密码不能为空");
        } else if (jiaoyan.test(newPassword)){
            $(".NewPasswordjiaoyanText").text("密码校验成功");
        } else{
            $(".NewPasswordjiaoyanText").text("密码校验失败");
        }
    });

    $("#secondaryPassword").blur(function () {
        var jiaoyan = new RegExp(/^[a-zA-Z0-9]{6}$/);
        var secondaryPassword = $("input[name=secondaryPassword]").val();
        if (jiaoyan.test(secondaryPassword)){
            if ($("input[name=newPassword]").val() == $("input[name=secondaryPassword]").val()) {
                $(".NewSecondaryPasswordjiaoyanText").text("密码校验成功");
            }else{
                $(".NewSecondaryPasswordjiaoyanText").text("两次密码不相同");
            }
        }

    });
    $("#refresh").click(function () {
        location.reload();
    });
    $(".queding").click(function () {

        var NewSecondaryPasswordjiaoyanText = $(".NewSecondaryPasswordjiaoyanText").text();
        var NewPasswordjiaoyanText = $(".NewPasswordjiaoyanText").text();



        if (NewSecondaryPasswordjiaoyanText == "密码校验成功" && NewPasswordjiaoyanText == "密码校验成功") {
            var newPassword = $("input[name=newPassword]").val();
            var secondaryPassword = $("input[name=secondaryPassword]").val();
            if (newPassword != null && newPassword!='' && secondaryPassword != null && secondaryPassword!=''){
                if (newPassword == secondaryPassword) {
                    var param = {
                        "userId":user.data.userId,
                        "password":newPassword
                    }
                    $.ajax({
                        url:"/HomePage/UpdateUser",
                        data:param,
                        type:'post',
                        dataType:"json",
                        success:function (data) {
                            if (data.data == true){
                                layer.msg("修改成功");
                            }
                        }
                    });
                }else{
                    layer.msg("两次输入密码不同");
                }
            } else{
                layer.msg("密码不能为空");
            }
        }

    });
})